home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / TinyGL / ami / content / ad709 / tinygl / src / specbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-15  |  1.7 KB  |  70 lines

  1. /*$T specbuf.c GC 1.137 08/09/02 17:47:18 */
  2.  
  3. /*$6
  4.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  5.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  6.  */
  7.  
  8. #include "zgl.h"
  9. #include "msghandling.h"
  10. #include <math.h>
  11. #include <stdlib.h>
  12.  
  13. /* */
  14.  
  15. static void calc_buf(GLSpecBuf *buf, const float shininess) {
  16.     int        i;
  17.     float    val, inc;
  18.     /*~~~~~~~~~~~~~*/
  19.  
  20.     val = 0.0f;
  21.     inc = 1.0f / SPECULAR_BUFFER_SIZE;
  22.     for(i = 0; i <= SPECULAR_BUFFER_SIZE; i++) {
  23.         buf->buf[i] = pow(val, shininess);
  24.         val += inc;
  25.     }
  26. }
  27.  
  28. /* */
  29. GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, const float shininess) {
  30.     GLSpecBuf    *found, *oldest;
  31.     found = oldest = c->specbuf_first;
  32.     while(found && found->shininess_i != shininess_i) {
  33.         if(found->last_used < oldest->last_used) {
  34.             oldest = found;
  35.         }
  36.  
  37.         found = found->next;
  38.     }
  39.  
  40.     if(found) { /* hey, found one! */
  41.         found->last_used = c->specbuf_used_counter++;
  42.         return found;
  43.     }
  44.  
  45.     if(oldest == NULL || c->specbuf_num_buffers < MAX_SPECULAR_BUFFERS) {
  46.         /* create new buffer */
  47.         GLSpecBuf    *buf = malloc(sizeof(GLSpecBuf));
  48.         if(!buf) {
  49.             gl_fatal_error("could not allocate specular buffer");
  50.         }
  51.  
  52.         c->specbuf_num_buffers++;
  53.         buf->next = c->specbuf_first;
  54.         c->specbuf_first = buf;
  55.         buf->last_used = c->specbuf_used_counter++;
  56.         buf->shininess_i = shininess_i;
  57.         calc_buf(buf, shininess);
  58.         return buf;
  59.     }
  60.  
  61.     /*
  62.      * overwrite the lru buffer £
  63.      * tgl_trace("overwriting spec buffer :(\n");
  64.      */
  65.     oldest->shininess_i = shininess_i;
  66.     oldest->last_used = c->specbuf_used_counter++;
  67.     calc_buf(oldest, shininess);
  68.     return oldest;
  69. }
  70.